package com.yyge.dao.Impl;

import com.yyge.dao.BaseDao;
import com.yyge.dao.UserInfoDao;
import com.yyge.entity.Userinfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserInfoDaoImpl extends BaseDao implements UserInfoDao {
    @Override
    public int add(Userinfo userinfo) {
        String SQL="insert into userInfo value(default,?,?,?,?,?,?,?)";
        int count=0;
        try {
            super.open_db();
            super.sta=conn.prepareStatement(SQL);
            System.out.println(userinfo);
            System.out.println("注册账号..");
            sta.setString(1,userinfo.getName());
            sta.setString(2,userinfo.getPassword());
            sta.setString(3,userinfo.getAge()+"");
            sta.setString(4,userinfo.getSex()+"");
            sta.setString(5,userinfo.getPhone());
            sta.setString(6,userinfo.getAvatar());
            sta.setString(7,userinfo.getToken());
            count=sta.executeUpdate();
        } catch (SQLException e) {
            count=0;
           throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
        return count;
    }

    @Override
    public int modify(Userinfo userinfo) {
        System.out.println("我会来修改用户");
        String SQL="update userinfo set name=?,password=?,age=?,sex=?,phone=?,avatar=?,token=? where id=?";
        int count=0;
        try {
            super.open_db();
            sta=conn.prepareStatement(SQL);
            System.out.println(SQL);
            System.out.println(userinfo);
            sta.setString(1,userinfo.getName());
            sta.setString(2,userinfo.getPassword());
            sta.setInt(3,userinfo.getAge());
            sta.setInt(4,userinfo.getSex());
            sta.setString(5,userinfo.getPhone());
            sta.setString(6,userinfo.getAvatar());
            sta.setString(7,userinfo.getToken());
            sta.setInt(8,userinfo.getId());
            count=sta.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
            System.out.println("我会来修改用户修改后返回值...");
            System.out.println(count);
            return count;
        }
    }

    @Override
    public int remove(Integer id) {
        String SQL="delete  from userinfo where id=?";
        int count=0;
        try {
            super.open_db();
            sta=conn.prepareStatement(SQL);
            sta.setInt(1,id);
            count=sta.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
            return count;
        }
    }

    @Override
    public List<Userinfo> findAll() {
        String SQL = "select * from userinfo";
        System.out.println(SQL);
        List<Userinfo> list=new ArrayList<>();
        try {
            super.open_db();
            sta=conn.prepareStatement(SQL);
            rs=sta.executeQuery();
            while (rs.next()){
                System.out.println(rs);
                Userinfo user=new Userinfo();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getInt("sex"));
                user.setAge(rs.getInt("age"));
                user.setToken(rs.getString("token"));
                user.setPhone(rs.getString("phone"));
                user.setAvatar(rs.getString("avatar"));

                System.out.println(user);
                list.add(user);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
            return list;
        }
    }

    @Override
    public Userinfo findbyId(Integer id) {
     String SQL="select * from userinfo where id=?";
     Userinfo userinfo=new Userinfo();
        try {
           super.open_db();
           sta=conn.prepareStatement(SQL);
           sta.setInt(1,id);
           rs=sta.executeQuery();
           while (rs.next()){
               //装配其他信息
               userinfo.setId(rs.getInt("id"));
               userinfo.setPassword(rs.getString("password"));
               userinfo.setName(rs.getString("name"));
               userinfo.setAge(rs.getInt("age"));
               userinfo.setSex(rs.getInt("sex"));
               userinfo.setPhone(rs.getString("phone"));
               userinfo.setAvatar(rs.getString("avatar"));
               userinfo.setToken(rs.getString("token"));
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
         return userinfo;
    }

    @Override
    public int query(Userinfo userinfo) {
        String SQL="update userinfo set name=?,password=?,age=?,sex=?,phone=?,avatar=? where name=?";
        int count=0;
        try {
            super.open_db();
            sta=conn.prepareStatement(SQL);
            sta.setString(1,userinfo.getName());
            sta.setString(2,userinfo.getPassword());
            sta.setInt(3,userinfo.getAge());
            sta.setInt(4,userinfo.getSex());
            sta.setString(5,userinfo.getPhone());
            sta.setString(6,userinfo.getAvatar());
            count=sta.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
            return count;
        }
    }


    @Override
    public Userinfo Logon(Userinfo use) {
     String SQL="select * from userinfo where name=?";
        try {
           super.open_db();
           sta=conn.prepareStatement(SQL);
           sta.setString(1,use.getName());
           //sta.setString(2,use.getPassword());
           rs=sta.executeQuery();
           while (rs.next()){
               //装配其他信息
               use.setId(rs.getInt("id"));
               use.setAge(rs.getInt("age"));
               use.setPassword(rs.getString("password"));
               use.setSex(rs.getInt("sex"));
               use.setPhone(rs.getString("phone"));
               use.setAvatar(rs.getString("avatar"));
               use.setToken(rs.getString("token"));
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
         return use;
    }
}
